﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using MessageSystem.Managed.Interface;
using MessageSystem.Managed.Infrastructure;

namespace MessageSystem.Managed.Http
{
    class Acceptor
    {
        public void Listen(MessageSystem ms,string url,OnAccept a)
        {
            m_pMessageSystem = ms;
            m_pAccept = a;
            m_listener = new HttpListener();
            m_listener.Prefixes.Add(url);
            m_listener.Start();

            m_listener.BeginGetContext(OnEndGetContext, this);
        }

        private void OnEndGetContext(IAsyncResult ar)
        {
            try
            {
                HttpListenerContext context = m_listener.EndGetContext(ar);
                HttpListenerRequest request = context.Request;
                HttpListenerResponse response = context.Response;

                byte[] inputbuf = new byte[request.InputStream.Position];
                request.InputStream.Read(inputbuf, 0, inputbuf.Length);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            m_listener.BeginGetContext(OnEndGetContext, this);
        }

        private HttpListener m_listener;
        private OnAccept m_pAccept;
        private MessageSystem m_pMessageSystem;
    }
}
